using System.Collections;
using System.Collections.Generic;
using Framework;
using UnityEngine;

public class GameWorld : FsmStateAction, IActionInputCallback
{
    private Player _player;
    private MapGrid _map;
    private float _getActorInterval;
    private GameContext _gameContext;

    public void OnActionHoldPerform(InputAction action)
    {
    }


    public void OnActionInputCancel(InputAction action)
    {
        if (action.name == GameInputName.Move)
        {
            _player.MoveStop();
        }
        else if (action.name == GameInputName.Action_A)
        {
            if (_player.InteractiveTarget != null)
            {
                _player.Action();
            }
        }

    }

    public void OnActionInputPerform(InputAction action)
    {
        if (action.name == GameInputName.Move)
        {
            var v2 = action.value2;

            var dir = new Vector3(v2.x, 0, v2.y);
            if (dir.sqrMagnitude < 0.01f)
            {
                _player.MoveStop();
            }
            else
            {
                _player.Move(dir, Time.deltaTime);
            }
            _map.Add(_player);
        }

    }

    public void OnActionInputStart(InputAction action)
    {
    }


    protected override void OnAwake()
    {
        base.OnAwake();
    }
    protected override void OnEnter()
    {
        base.OnEnter();
        _gameContext = GameEntry.Context;
        _map = _gameContext.World.MapGrid;
        _gameContext.ActionInput = this;
        _getActorInterval = 0.2f;
        GameEntry.Coroutine.Start(load());
        GameEntry.GUI.Open(GameEntry.GUIPath.LoadingUI);

    }
    protected override void OnExit()
    {
        base.OnExit();
        if (_player != null)
        {
            _player.MoveStop();
            _player = null;
        }
        GameEntry.DeviceManager.RemoveActionCallback(this);
    }
    protected override void OnUpdate(float deltaTime, float progress)
    {
        base.OnUpdate(deltaTime, progress);
#if UNITY_EDITOR
        //_map.Draw();
#endif
        if (_player != null && _player.IsGameObjectEnabled)
        {
            var input = GameEntry.Context.PlayerInput;
            /*
            while (true)
            {
                var action = input.Read();
                if (action == null)
                {
                    break;
                }
                if (action.Name == GameInputName.Move)
                {
                    if (action.Phase == GameDeviceActionPhase.Performed)
                    {
                        var v2 = action.DataValue2;

                        var dir = new Vector3(v2.x, 0, v2.y);
                        if (dir.sqrMagnitude < 0.01f)
                        {
                            _player.MoveStop();
                        }
                        else
                        {
                            _player.Move(dir, deltaTime);
                        }
                        _map.Add(_player);
                    }
                    else if (action.Phase == GameDeviceActionPhase.Canceled)
                    {
                        _player.MoveStop();
                    }
                }
                else if (action.Name == GameInputName.Action_A)
                {
                    if (action.Phase == GameDeviceActionPhase.Started)
                    {

                    }
                    else if (action.Phase == GameDeviceActionPhase.Performed)
                    {
                        if (_player.InteractiveTarget != null)
                        {
                            _player.Action();
                        }
                    }
                    else if (action.Phase == GameDeviceActionPhase.Canceled)
                    {
                        if (_player.InteractiveTarget != null)
                        {
                            _player.Action();
                        }
                    }
                }
            }
            */
            if (_getActorInterval > 0f)
            {
                _getActorInterval -= deltaTime;
                if (_getActorInterval <= 0f)
                {
                    var searchTarget = _gameContext.World.MapGrid.GetNearest(_player, 2f, 0.4f);
                    _player.InteractiveTarget = searchTarget;
                    _gameContext.World.ActiveActor = searchTarget;
                    GameEntry.GUI.SetParam(GameEntry.GUIPath.JoystickUI, "updateActiveActor");
                    _getActorInterval = 0.2f;
                }
            }

        }


        if (Input.GetKeyDown(KeyCode.R))
        {
            GameEntry.Entity.ClearPool<Test>("Assets/Res/Prefabs/Avatar/DogPolyart.prefab", 5);
        }
    }
    IEnumerator load()
    {
        // for (var i = 0; i < 10; i++)
        // {
        //     var res = GameEntry.Entity.Add<Res>("Assets/Res/Prefabs/Res/Apple.prefab");
        //     res.Pos = _map.GetGroundPosition(new Vector3(Random.Range(-20, 20), 0, Random.Range(-20, 20)));

        // }
        // for (var i = 0; i < 10; i++)
        // {
        //     var res = GameEntry.Entity.Add<Res>("Assets/Res/Prefabs/Res/Strawberry.prefab");
        //     res.RandomRotation = true;
        //     res.Pos = _map.GetGroundPosition(new Vector3(Random.Range(-20, 20), 0, Random.Range(-20, 20)));
        // }
        Random.InitState(10086);
        for (int i = 0; i < 5; i++)
        {
            var res = GameEntry.Entity.Add<Tree>("Assets/Res/Prefabs/Res/Tree1.prefab");
            res.SetCount(Random.Range(1, 4));
            res.Pos = _map.GetGroundPosition(new Vector3(Random.Range(-20, 20), 5f, Random.Range(-20, 20)));
        }
        var config = SceneConfig.Instance;
        var enterId = _gameContext.SceneEnterId;

        _player = GameEntry.Entity.Add<Player>("Assets/Res/Prefabs/Avatar/DogPolyart.prefab");
        while (!_player.IsGameObjectEnabled)
        {
            yield return null;
        }
        _map.Add(_player);
        _player.MoveStop();
        GameEntry.Context.World.Player = _player;
        var virtualCameara = config.VirtualCamera;
        var entry = config.EnterPoint[_gameContext.SceneEnterId];
        if (entry != null)
        {
            _player.WarpTo(entry.position);
            virtualCameara.transform.position = entry.position;
            virtualCameara.enabled = false;
            _player.GetDisplay().transform.rotation = entry.rotation;
        }
        yield return null;
        if (virtualCameara != null)
        {
            virtualCameara.Follow = _player.GetDisplay().transform;
            virtualCameara.enabled = true; ;
        }
        GameEntry.GUI.Open(GameEntry.GUIPath.JoystickUI);
        GameEntry.GUI.Open(GameEntry.GUIPath.MenuUI);
        var list = new List<Res>();
        GameEntry.Entity.GetEntitys<Res>(list);
        foreach (var item in list)
        {
            _map.Add(item);
        }
        // yield return new WaitForSeconds(1f);
        GameEntry.GUI.Close(GameEntry.GUIPath.LoadingUI);
        GameEntry.DeviceManager.AddActionCallback(this);
    }
}